﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.HocKyService;
namespace eTMS.Controllers{
public class HocKyControllerBase : CommonController{
	public HocKyControllerBase()
		: base()
	{	
		this.dao = new HocKyServiceClient();
	}
	~HocKyControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private HocKyEntity oHocKy;	
	public HocKyEntity HocKyObject{
		get{
			return oHocKy;
		}
		set{
			oHocKy=value;
		}
	}
	protected HocKyServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSHocKy(false);			
			return _Table;
		}
	}
	public void LoadAllDSHocKy(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oHocKy==null)return false;
		bool result=true;
		try{
			oHocKy = dao.SaveRet(oHocKy);
		}
		catch{
			result=false;
		}
		if(result){
			_ListHocKy.Add(oHocKy);
		}
		return result;
	}
	public bool Update(){
		if(oHocKy==null)return false;
		return dao.Update(oHocKy);
	}
	public bool Delete(){
		if(oHocKy==null)return false;
		bool retvalue = dao.Delete(oHocKy);
		if(retvalue){			
			_ListHocKy.Remove(oHocKy);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 HocKy_ID){
		bool retvalue = dao.DeleteByID(HocKy_ID);		
		if(retvalue){
			HocKyEntity oDeleted=FindHocKy(HocKy_ID);
			if(oDeleted!=null){
				_ListHocKy.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oHocKy = new HocKyEntity();
	}
	//Check if exists MaHocKy
	public bool CheckExistsMaHocKy(object value){	
		return dao.SelectOneByMaHocKy(value.ToString()).MaHocKy!=null;
    }    
    //Copy data from data row in grid to object
    public HocKyEntity MapDataRowToObject(DataRow row){
		HocKyEntity o = new HocKyEntity();
		 o.MaHocKy=Convert.ToString(row["MaHocKy"]);
		 o.TenHocKy=Convert.ToString(row["TenHocKy"]);
		 o.MoTa=Convert.ToString(row["MoTa"]);
		 if(!row.IsNull("ThuTu"))
			o.ThuTu=ConvertInt(row["ThuTu"]);
		 if(!row.IsNull("HocKyHienTai"))
			o.HocKyHienTai=ConvertBool(row["HocKyHienTai"]);
		 if(!row.IsNull("HocKy_ID"))
		    o.HocKy_ID=(Int32)(row["HocKy_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(HocKyEntity o,DataRow row){
		row["MaHocKy"]=o.MaHocKy;
		row["TenHocKy"]=o.TenHocKy;
		row["MoTa"]=o.MoTa;
		row["ThuTu"]=o.ThuTu;
		row["HocKyHienTai"]=o.HocKyHienTai;
		row["HocKy_ID"]=o.HocKy_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.HocKyObject,row);
    }
	public void LoadObject(Int32 HocKy_ID){
		HocKyEntity oLoad=FindHocKy(HocKy_ID);
		if(oLoad==null){
			oHocKy=this.LoadHocKy(HocKy_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oHocKy.IsNullHocKy_ID;
			if(!bNotIsNull){
				this.ListHocKy.Add(oHocKy);
			}
		}
		else{
			oHocKy=oLoad;
		}
	}
	#region CachedList HocKy
	private static List<HocKyEntity> _ListHocKy=new List<HocKyEntity>();
    public List<HocKyEntity>ListHocKy{
		get{
			return _ListHocKy;
		}
	}
	public static List<HocKyEntity>ListHocKyStatic{
		get{
			return _ListHocKy;
		}
	}
	public HocKyEntity FindHocKy(Int32 HocKy_ID){
		HocKyEntity oResult=this.ListHocKy.Find(delegate(HocKyEntity oFind){
			bool result=true; 
			result&=oFind.HocKy_ID.Equals(HocKy_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllHocKy(bool clearCache){
		if(_ListHocKy.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		HocKyServiceClient dao = new HocKyServiceClient();
		_ListHocKy=new List<HocKyEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public HocKyEntity LoadHocKy(Int32 HocKy_ID){			
		HocKyEntity o=dao.SelectOne(HocKy_ID);		
		return o;
	}
	#endregion
}
}
